//
//  FriendRequestsViewController.m
//  AAIM
//
//  Created by awh on 2019/12/12.
//  Copyright © 2019年 AAIM. All rights reserved.
//

#import "FriendRequestsViewController.h"
#import "ContactRequestLogic.h"
#import "BTMessageModel.h"
@interface FriendRequestsViewController ()
@property(nonatomic,strong)UIScrollView * bgScrollView;
@property(nonatomic,strong)UIImageView * headImage;
@property(nonatomic,strong)UILabel * friendName;
//@property(nonatomic,strong)UILabel * friendAccount;
@property(nonatomic,strong)UILabel * friendID;
@property(nonatomic,strong)UIView * contentback;
@property(nonatomic,strong)UILabel * contentlbl;

@property(nonatomic,strong)UIButton * sendButton;
@end

@implementation FriendRequestsViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.gk_navTitle=@"好友申请";
    
    [self addSubViews];
    
}
-(void)addSubViews
{
    [self.view addSubview:self.bgScrollView];
    [self.bgScrollView addSubview:self.headImage];
    [self.bgScrollView addSubview:self.friendName];
//    [self.bgScrollView addSubview:self.friendAccount];
    [self.bgScrollView addSubview:self.friendID];
    [self.bgScrollView addSubview:self.contentback];
    [self.contentback addSubview:self.contentlbl];
    [self.bgScrollView addSubview:self.sendButton];
    
    //    placeholderImage:kHeadPlaceholderImage]
    
    [_headImage sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@%@",userHeaderImage,_model.fhead]] placeholderImage:[UIImage imageNamed:@"bg_pyqtx"] completed:^(UIImage * _Nullable image, NSError * _Nullable error, SDImageCacheType cacheType, NSURL * _Nullable imageURL) {
        KWeakSelf;
        weakSelf.headImage.image = image;
    }];
    _friendName.text = _model.fnickname;
    //    _friendID.text = _model.
    //    _friendAccount.text = _model.
    _contentlbl.text = _model.applyContent;
    
}

-(UIScrollView*)bgScrollView
{
    if (!_bgScrollView) {
        _bgScrollView=[[UIScrollView alloc]initWithFrame:CGRectMake(0,NavigationBarHeight, SCREEN_WIDTH,SCREEN_HEIGHT-NavigationBarHeight-EMVIEWBOTTOMMARGIN)];
        _bgScrollView.showsVerticalScrollIndicator=NO;
        _bgScrollView.showsHorizontalScrollIndicator=NO;
        _bgScrollView.backgroundColor=[UIColor whiteColor];
        [_bgScrollView setContentSize:CGSizeMake(0, kScreenHeight)];
    }
    return _bgScrollView;
}
-(UIImageView*)headImage
{
    if (!_headImage) {
        _headImage=[[UIImageView alloc]initWithFrame:CGRectMake(15, 25, 70, 70)];
        _headImage.layer.masksToBounds=YES;
        _headImage.layer.cornerRadius=35;
        _headImage.backgroundColor=[UIColor redColor];
        
    }
    return _headImage;
}
-(UILabel*)friendName
{
    if (!_friendName) {
        _friendName=[self CreateLabelWithFrame:CGRectMake(_headImage.right+15, _headImage.y+15, SCREEN_WIDTH-_headImage.right-30, 18) Font:15 titleColor:KHEXCOLOR(@"#262B2F")];
        _friendName.text=@"刚女孩";
    }
    return _friendName;
}
//-(UILabel*)friendAccount
//{
//    if (!_friendAccount) {
//        _friendAccount=[self CreateLabelWithFrame:CGRectMake(_friendName.x, _friendName.bottom+3, _friendName.width, _friendName.height) Font:12 titleColor:KHEXCOLOR(@"#AFAFAF")];
//        _friendAccount.text=@"账号：39282131";
//    }
//    return _friendAccount;
//}
-(UILabel*)friendID
{
    if (!_friendID) {
        _friendID=[self CreateLabelWithFrame:CGRectMake(_friendName.x, _friendName.bottom+3, _friendName.width, _friendName.height) Font:12 titleColor:KHEXCOLOR(@"#AFAFAF")];
        _friendID.text=@"ID：392822";
    }
    return _friendID;
}

-(UILabel*)CreateLabelWithFrame:(CGRect)Frame Font:(CGFloat)font titleColor:(UIColor*)titleColor
{
    UILabel * lbl=[[UILabel alloc]initWithFrame:Frame];
    lbl.font=[UIFont systemFontOfSize:font];
    lbl.textColor=titleColor;
    return lbl;
}
-(UIView*)contentback
{
    if (!_contentback) {
        _contentback=[[UIView alloc]initWithFrame:CGRectMake(_headImage.x, _headImage.bottom+15, SCREEN_WIDTH-30, 160)];
        _contentback.backgroundColor=KHEXCOLOR(@"#F5F5F5");
    }
    return _contentback;
}
-(UILabel*)contentlbl
{
    if (!_contentlbl) {
        _contentlbl=[self CreateLabelWithFrame:CGRectMake(10, 10, _contentback.width-20, _contentback.height) Font:13 titleColor:KHEXCOLOR(@"#262B2F")];
        _contentlbl.text=@"你好，我是钢铁女孩，很高兴认识你…";
        [_contentlbl sizeToFit];
        _contentlbl.numberOfLines = 0;
    }
    return _contentlbl;
}
-(UIButton*)sendButton
{
    if (!_sendButton) {
        _sendButton=[UIButton buttonWithType:UIButtonTypeCustom];
        _sendButton.frame=CGRectMake(_headImage.x, _contentback.bottom+50, SCREEN_WIDTH-30, 40);
        _sendButton.backgroundColor= [_model.isAgree integerValue]==1?[UIColor grayColor]:KHEXCOLOR(AllBackColor_bule );
        _sendButton.layer.masksToBounds=YES;
        _sendButton.layer.cornerRadius = 5;
        [_sendButton setTitle:[_model.isAgree integerValue]==1?@"已接受":@"接受" forState:UIControlStateNormal];
        [_sendButton setUserInteractionEnabled:[_model.isAgree integerValue]==1?NO:YES];
        [_sendButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        _sendButton.titleLabel.font=[UIFont systemFontOfSize:15];
        [_sendButton addTarget:self action:@selector(didButtnAction:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _sendButton;
}
-(void)didButtnAction:(UIButton*)sender
{
    [ContactRequestLogic requestAgreedFriendWithID:_model.fuuid isAgree:@"1" Success:^(NSDictionary * _Nonnull Dict) {
        KWeakSelf;
        NSLog(@"同意申请：%@",Dict);
        NSLog(@"同意好友，更新本地好友数据，发送im告知对方");
        [[UserDataBaseTool shareUserDataBaseTool] reloadData:1 Success:^(BOOL isOK) {
            if (isOK) {
                [[NSNotificationCenter defaultCenter] postNotificationName:kMessageSystemAddFriends object:nil];
            }
        }];
        [sender setTitle:@"已接受" forState:UIControlStateNormal];
        [sender setUserInteractionEnabled:NO];
        sender.backgroundColor= [UIColor grayColor];
        [CommonTools changeNewFriendsStatus:1 withFriendID:weakSelf.model.friendID];
        // 存储数据库
        BTMessageModel *textModel = [BTMessageModel new];
        textModel.msgtype = MessageTypeText;
        textModel.messageDirection = MessageDirectionSend;
        textModel.fuuid = [LoginManager shareLoginManager].userModel.fuuid;
        textModel.f_uuid = [LoginManager shareLoginManager].userModel.fuuid;
        long long timestamp =  [ChatTool shareChatTool].currentTimestamp;
        textModel.timestamp = timestamp;
        textModel.head = [LoginManager shareLoginManager].userModel.head;
        textModel.username = [LoginManager shareLoginManager].userModel.nickname;
        textModel.rfuuid =  weakSelf.model.fuuid;
        textModel.chatType = ChatTypeFriend;
        textModel.gId = weakSelf.model.fuuid;
        textModel.nickname = [LoginManager shareLoginManager].userModel.nickname;
        //               textModel.messageStatus = MessageStatusDelivering;
        textModel.type_id=SystemMessageAgreeAddFriend;
        textModel.addfriendState = 1;
        [[ConversationListDataBase shareConversationListDataBase] saveConversation:textModel];
        [[ConversationManager shareManager] saveConversation:textModel];
        [self showTipHudWithText:Dict[@"message"]];
    } failure:^(NSString * _Nonnull errMsg) {
        [self showTipHudWithText:errMsg];
    }];
    
}
@end
